package com.injurydetection.www.control;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MenuItem;
import android.widget.PopupMenu;
import android.widget.TextView;

import androidx.annotation.MenuRes;
import androidx.core.content.ContextCompat;

import com.injurydetection.www.R;
import com.injurydetection.www.view.ExamineListFragment;

/**
 * Created by sunguiyong on 2020/8/13
 */
public class ExamineListControl {
    /**
     * 显示popumenu和点击监听
     *
     * @param context
     * @param textView
     * @param menuRes
     */
    public static void showPopu(Context context, TextView textView, @MenuRes int menuRes,ItemSelect itemSelect) {
        android.widget.PopupMenu popupMenu = new android.widget.PopupMenu(context, textView);
        popupMenu.getMenuInflater().inflate(menuRes, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                textView.setText(item.getTitle().toString());
                textView.setTextColor(context.getResources().getColor(R.color.white));
                itemSelect.onItemSelect(item);
                return true;
            }
        });
        popupMenu.show();
        change(context, textView, true);
        ExamineListFragment.setIsChoose(true);
        popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
            @Override
            public void onDismiss(PopupMenu menu) {
                ExamineListFragment.setIsChoose(false);
                change(context, textView, false);
            }
        });
    }

    public static void change(Context context, TextView textView, boolean select) {
        if (select) {
            Drawable drawable = ContextCompat.getDrawable(context, R.drawable.icon_arrow_select);
            drawable.setBounds(0, 0, 45, 45);
            textView.setCompoundDrawables(null, null, drawable, null);
        } else {
            Drawable drawable = ContextCompat.getDrawable(context, R.drawable.icon_arrow);
            drawable.setBounds(0, 0, 45, 45);
            textView.setCompoundDrawables(null, null, drawable, null);
        }
    }

    public interface ItemSelect{
        void onItemSelect(MenuItem item);
    }

}
